//
//  JXShakeCtl.m
//  JiaoX
//
//  Created by yinqy on 15/4/22.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXShakeCtl.h"
#import "POPBasicAnimation.h"
#import "HomeDataTool.h"
#import "ShakeList.h"
#import "CustomShowView.h"
#import "Shake.h"
#import "ShakeEmpty.h"
#import "ShakeTen.h"
#import "ShakeProd.h"
#import "ShakePost.h"
#import "ShakeTomorrow.h"
#import "JXPostsDetailCtl.h"
#import "JXProdDetailCtl.h"
#import "AudioToolbox/AudioToolbox.h"

@interface JXShakeCtl ()
@property (nonatomic,strong)NSArray *shakeList;
@property (nonatomic,assign)NSInteger currentIndex;
@property (nonatomic,strong)CustomShowView *showView;
@property (nonatomic,assign)BOOL canNotShake;//不可以摇，默认为no，可以摇
@end

@implementation JXShakeCtl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.bottomView setCornerRadius:self.bottomView.height/2 borderWidth:2 borderColor:JXColor(204, 82, 82)];
    [[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];
    [self setTitle:@"摇一摇"];
    [self resetStatus];
}

-(void)getShakeList{
    [HomeDataTool getUserShakeList:^(ShakeList *result) {
        if (ISBLANK(result.data) && result.shakeList.count>0) {
            self.shakeList = result.shakeList;
            [self textSwitchIn];
        }
    } failure:^(NSError *error) {
    }];
}

-(void)textSwitchOut{
    [self.bottomLabel setAlpha:1];
    [self.bottomLabel setY:0];
    [UIView animateWithDuration:0.5 animations:^{
        [self.bottomLabel setY:-self.bottomLabel.height];
        [self.bottomLabel setAlpha:0];
    } completion:^(BOOL finished) {
        [self textSwitchIn];
    }];
}

-(void)textSwitchIn{
    self.currentIndex ++;
    if (self.currentIndex>=self.shakeList.count) {
        self.currentIndex=0;
    }
    [self.bottomLabel setText:self.shakeList[self.currentIndex]];
    [self.bottomLabel setAlpha:0];
    [self.bottomLabel setY:self.bottomLabel.height];
    [UIView animateWithDuration:0.5 animations:^{
        [self.bottomLabel setY:0];
        [self.bottomLabel setAlpha:1];
    } completion:^(BOOL finished) {
        [self performSelector:@selector(textSwitchOut) withObject:nil afterDelay:5];
    }];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
    [self getShakeList];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
}


- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    
    if (!self.canNotShake) {
        self.canNotShake = YES;
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        [self animationWithDuration:0.05];
        if (self.showView) {
            [self.showView hideWithanim:NO];
            self.showView = nil;
        }
        [self performSelector:@selector(shake) withObject:nil afterDelay:1];
    }

}

- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    //摇动取消
    [self animationWithDuration:0.5];
}

- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    //摇动结束
    [self performSelector:@selector(resetStatus) withObject:nil afterDelay:1];
}

-(void)shake{
    [self resetStatus];
    MBProgressHUD *hud = [DialogUtils showLoadingWithView:nil];
    [HomeDataTool userShake:^(Shake *result) {
        self.canNotShake = NO;
        [hud hide:YES];
        if (ISBLANK(result.data)) {
            [self showDialogWithShake:result];
        }else{
            [DialogUtils showTipsWithView:self.navigationController.view String:result.data];
        }
    } failure:^(NSError *error) {
        self.canNotShake = NO;
        [DialogUtils showTipsWithView:self.navigationController.view String:@"加载失败"];
        [hud hide:YES];
    }];
}


-(void)showDialogWithShake:(Shake *)shake{
    if ([shake isRunOut]) {
        ShakeTomorrow *tomorrow = [[UINib nibWithNibName:@"ShakeTomorrow" bundle:[NSBundle mainBundle]] instantiateWithOwner:self options:nil][0];
        [tomorrow setCornerRadius:5 borderWidth:0 borderColor:nil];
        CustomShowView *showView = [[CustomShowView alloc]initWithCustomView:tomorrow];
        [showView showWithanim:YES];
        self.showView = showView;
    }else{
        UIView *alertView = nil;
        switch (shake.type) {
            case 1:{//商品
                ShakeProd *view = [[UINib nibWithNibName:@"ShakeProd" bundle:[NSBundle mainBundle]] instantiateWithOwner:self options:nil][0];
                [view setShake:shake];
                [view setOperation:^{
                    if (self.showView) {
                        [self.showView hideWithanim:NO];
                        self.showView = nil;
                    }
                    JXProdDetailCtl *prod = [[JXProdDetailCtl alloc]initWithPordId:shake.shakeId];
                    [self.navigationController pushViewController:prod animated:YES];
                }];
                alertView = view;
                break;
            }
            case 2:{//帖子
                ShakePost *view = [[UINib nibWithNibName:@"ShakePost" bundle:[NSBundle mainBundle]] instantiateWithOwner:self options:nil][0];
                [view setShake:shake];
                [view setOperation:^{
                    if (self.showView) {
                        [self.showView hideWithanim:NO];
                        self.showView = nil;
                    }
                    JXPostsDetailCtl *ctl = [[JXPostsDetailCtl alloc]initWithPostId:shake.shakeId];
                    [self.navigationController pushViewController:ctl animated:YES];
                }];
                alertView = view;
                break;
            }
            case 3:{//代金券
                ShakeTen *view = [[UINib nibWithNibName:@"ShakeTen" bundle:[NSBundle mainBundle]] instantiateWithOwner:self options:nil][0];
                [view.privilegeLabel setText:[NSString stringWithFormat:@"￥%@",shake.money]];
                [view setCornerRadius:5 borderWidth:0 borderColor:nil];
                alertView = view;

                break;
            }
            case 4:{//哑炮
                ShakeEmpty *view = [[UINib nibWithNibName:@"ShakeEmpty" bundle:[NSBundle mainBundle]] instantiateWithOwner:self options:nil][0];
                [view setCornerRadius:5 borderWidth:0 borderColor:nil];
                alertView = view;
                break;
            }
        }
        CustomShowView *showView = [[CustomShowView alloc]initWithCustomView:alertView];
        [showView showWithanim:YES];
        self.showView = showView;
    }
}

-(void)resetStatus{
    [self animationWithDuration:0.5];
}

-(void)animationWithDuration:(CGFloat)duration{
    CAAnimation *oldAnim = [self.planeCup.layer animationForKey:@"translationY"];
    if (oldAnim.duration!=duration) {
        [self.planeCup.layer removeAllAnimations];
    }else{
        return;
    }
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    anim.duration = duration; // 动画持续1秒
    anim.fromValue = @(-10);
    anim.toValue = @(10);
    anim.repeatCount = NSIntegerMax;
    anim.autoreverses = YES;
    [self.planeCup.layer addAnimation:anim forKey:@"translationY"];
}

-(BOOL)shouldLogin{
    return YES;
}

@end
